'use strict';

/**
 * @ngdoc function
 * @name webApp.controller:ImgcropCtrl
 * @description
 * # ImgcropCtrl
 * Controller of the webApp
 */
angular.module('webApp')
  .controller('ImgcropCtrl', function ($scope,popBox,promiseImgCrop,$location) {
    $scope.imageDataURI = '';
    $scope.resImageDataURI = '';
    //$scope.resImgQuality=1;
    $scope.selMinSize=300;
    $scope.resImgSize=64.5;
    $scope.cropData = '点击上传您的头像';
    $scope.onLoadDone=function() {
      $scope.submit = true;
      $scope.cropData = '重新选择';
    };
    $scope.onLoadError=function() {
      popBox.showConfirm('警告','头像上传失败！');
    };
    var handleFileSelect=function(evt) {
      var file = evt.currentTarget.files[0];
      var reader = new FileReader();
      reader.onload = function (evt) {
        $scope.$apply(function($scope){
          $scope.imageDataURI = evt.target.result;
        });
      };
      reader.readAsDataURL(file);
    };
    angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);
    //保存图片
    $scope.onChange = function(url){
      $scope.resImageDataURI = url;
    };
    $scope.saveImg = function(){
      promiseImgCrop.modifyImgCrop($scope.resImageDataURI).then(function(){
        popBox.showConfirm('提示','修改头像成功！');
        $scope.imageDataURI = '';
        $scope.submit = false;
        $location.path('/root/userData');
      });
    };
  });
